package com.coffee.ws.handler;

import com.coffee.ws.CoffeeWebsocketException;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class HandlerMapping {

    private final static Map<Integer, CoffeeTextHandler> textHandlerMapping = new HashMap<>();
    private final static Map<Integer, CoffeeChatHandler> chatHandlerMapping = new HashMap<>();
    private final static Map<Integer, CoffeeBinaryHandler> binaryHandlerMapping = new HashMap<>();

    @Getter
    private final static WebsocketHandlerGather handlerGather= new WebsocketHandlerGather();


    //------------get--------------------------
    public static CoffeeTextHandler getTextHandler(Integer cmd) throws CoffeeWebsocketException {
        if (textHandlerMapping.isEmpty()){
            throw new CoffeeWebsocketException("Initialize MochaCmdHandler analysis handler error!");
        }
        return textHandlerMapping.get(cmd);
    }

    public static CoffeeChatHandler getChatHandler(Integer cmd) throws CoffeeWebsocketException {
        if (chatHandlerMapping.isEmpty()){
            throw new CoffeeWebsocketException("Initialize MochaChatHandler analysis handler error!");
        }
        return chatHandlerMapping.get(cmd);
    }

    public static CoffeeBinaryHandler getBinaryHandler(Integer cmd) throws CoffeeWebsocketException {
        if (binaryHandlerMapping.isEmpty()){
            throw new CoffeeWebsocketException("Initialize MochaChatHandler analysis handler error!");
        }
        return binaryHandlerMapping.get(cmd);
    }


    public static CoffeeConnectHandler getConnectHandler(){
        return handlerGather.getConnectHandler();
    }

    public static CoffeeCloseHandler getCloseHandler(){
        return handlerGather.getCloseHandler();
    }

    public static CoffeeUserValidHandler getUserValidHandler(){
        return handlerGather.getUserValidHandler();
    }



    //---------------------------------initialize------------------------

    public static void initializeConnectHandler(CoffeeConnectHandler connectHandler){
        handlerGather.setConnectHandler(connectHandler);
    }

    public static void initializeCloseHandler(CoffeeCloseHandler closeHandler){
        handlerGather.setCloseHandler(closeHandler);
    }

    public static void initializeUserValidHandler(CoffeeUserValidHandler userValidHandler){
        handlerGather.setUserValidHandler(userValidHandler);
    }

    public static void initializeTextHandler(Map<Integer, CoffeeTextHandler> handlerMap)throws Exception{
       if (handlerMap == null || handlerMap.isEmpty()){
           //throw new Exception("MochaTextHandler initializeMapping handlerMap can not be empty!");
           return;
       }
        textHandlerMapping.putAll(handlerMap);
    }

    public static void initializeBinaryHandler(Map<Integer, CoffeeBinaryHandler> handlerMap)throws Exception{
       if (handlerMap == null || handlerMap.isEmpty()){
           //throw new Exception("MochaBinaryHandler initializeMapping handlerMap can not be empty!");
           return;
       }
        binaryHandlerMapping.putAll(handlerMap);
    }

    public static void initializeChatHandler(Map<Integer, CoffeeChatHandler> handlerMap)throws Exception{
       if (handlerMap == null || handlerMap.isEmpty()){
           //throw new Exception("MochaChatHandler initializeMapping handlerMap can not be empty!");
           return;
       }
        chatHandlerMapping.putAll(handlerMap);
    }


}
